<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">新增围栏</span>
    <el-button type="primary" style="float:right;margin-top:13px;"
               @click="router.push('/geofence')">返回</el-button>
  </div>
  <div id="mapContainer" style="width:100%;height: 84vh;"></div>

  <el-dialog title="新建围栏" v-model="dialogVisible" :before-close="closeDialog">
    <el-form label-position="top">
      <el-form-item label="围栏名称">
        <el-input placeholder="请输入围栏名称" v-model="geofence.name"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="saveGeofence">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import router from "@/router";
import {nextTick, onMounted, ref} from "vue";
import { DrawScene, DrawControl,OperateEventType } from 'bmap-draw';
import {ElMessage} from "element-plus";
import qs from "qs";
import axios from "axios";

const geofence = ref({
  name:'',
  position:{}
});

var map;

onMounted(()=>{
  nextTick(()=>{
    map = new BMapGL.Map("mapContainer");
    let point = new BMapGL.Point(116.4074, 39.9024);
    map.centerAndZoom(point, 15);

    const scene = new DrawScene(map);
    const drawContrl = new DrawControl(scene, {
      enableTips: false,
      anchor: BMAP_ANCHOR_TOP_RIGHT,
      drawingItems: [
        'circle',
        'rectangle'
      ]
    });
    scene.addEventListener(OperateEventType.COMPLETE, (event) => {
      dialogVisible.value = true;
      console.log(event.target);
      let overlay = event.target.overlay;
      if(overlay instanceof BMapGL.Circle){
        geofence.value.position.type = 'circle';
        geofence.value.position.longitude = overlay.getCenter().lng;
        geofence.value.position.latitude = overlay.getCenter().lat;
        geofence.value.position.radius = overlay.getRadius();
        console.log(geofence.value);
      }else{
        geofence.value.position.type = 'rectangle';
        let recPoints = [];
        for(let i = 0; i < 4; i++){
          recPoints.push(overlay.points[i].latLng.lng +'-' + overlay.points[i].latLng.lat);
        }
        geofence.value.position.recPoints = recPoints.join(',');
        console.log(geofence.value);
      }

    });
    map.addControl(drawContrl);
  })
})

const dialogVisible = ref(false);
const closeDialog = () => {
  if(confirm('您确认要取消新增围栏吗?')){
    geofence.value = {name:'',position:{}};
    map.clearOverlays();
    dialogVisible.value = false;
  }
}
const saveGeofence = () => {
  if(!geofence.value.name || geofence.value.name.trim()==''){
    ElMessage.error('请输入围栏名称!');
    return;
  }
  geofence.value.position = JSON.stringify(geofence.value.position);
  let data = qs.stringify(geofence.value);
  axios.post(BASE_URL+'/v1/geofence/save',data)
      .then((response)=>{
        if (response.data.code === 2000){
          ElMessage.success('新增围栏成功!');
          dialogVisible.value = false;
          router.push('/geofence');
        }else {
          ElMessage.error(response.data.message);
        }
      })
}
</script>

<style scoped>

</style>